package com.access.boot.autoconfigure.apisdk.proxy.aop;


import com.access.boot.autoconfigure.apisdk.proxy.ProxyContainer;

/**
 * @author Yangzhen
 * @date 2022/6/2 17:11
 * @since 1.0.0
 */
public class ProxyFactory extends ApiSdkAdvisedSupport {

    public ProxyFactory(EnhanceProxyInstance enhanceProxyInstance) {
        setEnhanceProxyInstance(enhanceProxyInstance);
        setTarget(enhanceProxyInstance.getProxyBean());
        setInterfaces(new Class[]{enhanceProxyInstance.getOpenApiEnhance().getOpenApiInterface()});
    }

    public <T> T getProxy() {
        return (T) createAopProxy(this);
    }

    private synchronized <T> T createAopProxy(ApiSdkAdvisedSupport config) {
        T proxy = (T) new JdkDynamicAopProxy(config).getProxy();
        ProxyContainer.add(this.getProxiedInterfaces()[0], proxy);
        return proxy;
    }

}
